import os
from time import sleep

import allure

import page
from page.page_login import PageLogin
from page.page_study_manage import PageStudyManage
from tool.get_driver import GetDriver
from tool.get_log import GetLogger

log = GetLogger.get_log()
module_name = str(os.path.basename(__file__)).split('.')[0]

@allure.epic("我的页面")
@allure.feature("学习管理测试用例")
class TestStudyManage:
    @classmethod
    def setup_class(cls):
        cls.driver = GetDriver.get_app_driver()
        cls.login = PageLogin(cls.driver)
        cls.study = PageStudyManage(cls.driver)
        try:
            cls.login.base_click_element(page.approve)
        except:
            pass
        try:
            cls.login.base_click_element(page.skip_user_desc)
        except:
            pass
        cls.login.pagelogin(os.environ['STUDY_MANAGE_USERNAME'], password='123456')
        cls.login.base_click_element(page.wd_tab)

    @classmethod
    def teardown_class(cls):
        GetDriver.quit_app_driver()

    @allure.story("我的课程测试用例")
    def test01_study_manage_my_course(self):
        self.login.base_click_element(page.wd_tab)
        self.study.pagestudymanage_click_manage()
        self.study.pagestudymanage_click_my_course()
        if self.study.pagestudymanage_if_my_course1_exist():
            self.study.pagestudymanage_click_course1()
        else:
            self.study.pagestudymanage_add_course1()
            self.study.pagestudymanage_click_course1()
        log.info("正在进行断言")
        try:
            assert self.study.pagestudymanage_if_course1_success()
            log.info('断言成功')
            self.study.base_get_screenshot_as_file(module_name, '断言成功截图')
        except:
            log.error('断言失败')
            self.study.base_get_screenshot_as_file(module_name, '课程添加失败')
            raise
        finally:
            self.study.pagestudymanage_return_to_wdpage_keycode()

    @allure.title("人工复验")
    @allure.story("发声朗读测试用例——人工复验")
    def test02_study_manage_pronounce_3(self):
        self.study.pagestudymanage_click_manage()
        self.study.pagestudymanage_click_pronounce()
        self.study.pagestudymanage_click_3ci()
        try:
            log.info('断言成功')
            sleep(5)
            self.study.base_get_screenshot_as_file(module_name, '人工验证，是否点击为3次')
            assert True
        except:
            pass
        finally:
            self.study.pagestudymanage_return_to_wdpage_keycode()

    @allure.story("下载管理测试用例")
    def test03_study_manage_download(self):
        self.study.pagestudymanage_click_manage()
        self.study.pagestudymanage_click_download()
        if self.study.pagestudymanage_if_download1_exist():
            assert True
            log.info('断言成功')
            self.study.base_get_screenshot_as_file(module_name, '断言成功截图')
            # self.study.pagestudymanage_click_download1()
            self.study.pagestudymanage_return_to_wdpage_keycode()
        else:
            self.study.pagestudymanage_add_download1()
            # self.study.pagestudymanage_click_download1()
            log.info("正在进行断言")
            try:
                assert self.study.pagestudymanage_if_download1_exist()
                log.info('断言成功')
                self.study.base_get_screenshot_as_file(module_name, '断言成功截图')
            except:
                log.error('断言失败')
                self.study.base_get_screenshot_as_file(module_name, '下载视频添加失败')
                raise
            finally:
                self.study.pagestudymanage_return_to_wdpage_keycode()

    @allure.story("我的收藏测试用例")
    def test04_study_manage_my_collection(self):
        self.study.pagestudymanage_click_manage()
        self.study.pagestudymanage_click_my_collect()
        if self.study.pagestudymanage_if_my_collect_empty():
            title = self.study.pagestudymanage_add_my_collect_1()
            try:
                assert self.study.pagestudymanage_assert_add_collection(title)
                log.info('断言成功')
                self.study.base_get_screenshot_as_file(module_name, '断言成功截图')
            except:
                log.error('断言失败')
                self.study.base_get_screenshot_as_file(module_name, '收藏添加失败')
                raise
            finally:
                self.study.pagestudymanage_return_to_wdpage_keycode()
        else:
            log.info('断言成功')
            sleep(1)
            self.study.base_get_screenshot_as_file(module_name, '列表已有添加的收藏')
            assert True
            self.study.pagestudymanage_return_to_wdpage_keycode()